package com.sky.his.api.common;

import com.sky.his.api.exception.HisException;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.iai.v20180301.IaiClient;
import com.tencentcloudapi.iai.v20180301.models.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class FaceAuthUtil {

    @Value("${tencent.cloud.secretId")
    private String secretId;

    @Value("${tencent.cloud.secretKey}")
    private String secretKey;

    @Value("${tencent.cloud.face.groupId}")
    private String groupId;

    @Value("${tencent.cloud.face.region}")
    private String regin;

    public boolean verifyFaceModel(String name, String pid, String sex, String photo_1, String photo_2) {
        Credential cred = new Credential(secretId, secretKey);
        IaiClient client = new IaiClient(cred, regin);

        CompareFaceRequest compareFaceRequest = new CompareFaceRequest();
        compareFaceRequest.setImageA(photo_1);
        compareFaceRequest.setImageA(photo_2);
        CompareFaceResponse compareFaceResponse = null;

        boolean result = false;
        try {
            client.CompareFace(compareFaceRequest);
        } catch (TencentCloudSDKException e) {
            log.error("人脸比对失败", e);
            throw new HisException("人脸比对失败");
        }
        Float score = compareFaceResponse.getScore();
        if (score >= 50) {
            DetectLiveFaceRequest detectLiveFaceRequest = new DetectLiveFaceRequest();
            detectLiveFaceRequest.setImage(photo_2);
            DetectLiveFaceResponse detectLiveFaceResponse = null;

            try {
                client.DetectLiveFace(detectLiveFaceRequest);
            } catch (TencentCloudSDKException e) {
                log.error("静态活体识别", e);
                throw new HisException("静态活体识别失败");
            }
            result = detectLiveFaceResponse.getIsLiveness();
        } else {
            result = false;
            return result;
        }
        if (result) {
            GetPersonBaseInfoRequest getPersonBaseInfoRequest = new GetPersonBaseInfoRequest();
            getPersonBaseInfoRequest.setPersonId(pid);
            GetPersonBaseInfoResponse getPersonBaseInfoResponse = null;

            try {
                getPersonBaseInfoResponse = client.GetPersonBaseInfo(getPersonBaseInfoRequest);
            } catch (TencentCloudSDKException e) {
                log.error("查询人员库失败", e);
                throw new HisException("查询人员库失败");
            }
            String personName = getPersonBaseInfoResponse.getPersonName();
            if (personName == null) {
                CreatePersonRequest createPersonRequest = new CreatePersonRequest();
                createPersonRequest.setGroupId(groupId);
                createPersonRequest.setPersonId(pid);
                long gender = sex.equals("男") ? 1L : 2L;
                createPersonRequest.setGender(gender);
                createPersonRequest.setQualityControl(4L);
                createPersonRequest.setUniquePersonControl(4L);
                createPersonRequest.setImage(photo_1);
                CreatePersonResponse createPersonResponse = null;

                try {
                    createPersonResponse = client.CreatePerson(createPersonRequest);
                } catch (TencentCloudSDKException e) {
                    log.error("添加体检人道人员库失败", e);
                    throw new HisException("添加体检人道人员库失败");
                }
            }
        }
        return result;
    }

}
